package com.jinzhi.jzweb.controller;


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import com.jinzhi.jzweb.domain.BookOrderDO;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.SysBrandDO;
import com.jinzhi.jzweb.service.SysBrandService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2022-09-21 10:45:57 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/sysBrand")
public class SysBrandController extends BaseController {
	@Autowired
	private SysBrandService sysBrandService;

	@Autowired
	private UserService userService;

	@GetMapping()
	@RequiresPermissions("jzweb:sysBrand:sysBrand")
	String SysBrand(){
	    return "jzweb/sysBrand/sysBrand";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:sysBrand:sysBrand")
	public Result<Page<SysBrandDO>> list(SysBrandDO sysBrandDTO){
		// 查询列表数据
		EntityWrapper<SysBrandDO> wrapper = new EntityWrapper<>();
		if(StringUtils.isNotBlank(sysBrandDTO.getBrandName())){
			wrapper.eq("brand_name",sysBrandDTO.getBrandName()).orderBy("id");
		}

		Page<SysBrandDO> page = sysBrandService.selectPage(getPage(SysBrandDO.class),wrapper
					);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				SysBrandDO obj = (SysBrandDO) page.getRecords().get(i);
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:sysBrand:add")
	String add(){
	    return "jzweb/sysBrand/add";
	}

	@GetMapping("/brandDetails/{id}")
	@RequiresPermissions("jzweb:sysBrand:add")
	String brandDetails(@PathVariable("id") Long id,Model model){
		SysBrandDO sysBrand = sysBrandService.selectById(id);
		if(sysBrand.getUserId()!=null){
		UserDO userDO= userService.getByUserId(sysBrand.getUserId());
		if(userDO!=null){
			sysBrand.setUserIdName(userDO.getUsername());
		}

		}
		if(sysBrand.getToExamine()!=null){
			sysBrand.setToExamineName(userService.getByUserId(sysBrand.getToExamine()).getUsername());
		}
		if (sysBrand.getStatus()==0){
			sysBrand.setStatusNmae("未审核");
		}else if(sysBrand.getStatus()==1){
			sysBrand.setStatusNmae("审核通过");
		}else
		{
			sysBrand.setStatusNmae("审核未通过");
		}
		if (sysBrand.getState()==0){
			sysBrand.setStateNmae("正常");
		}else {
			sysBrand.setStateNmae("删除/作废");
		}
		if(sysBrand.getCreateTime()!=null){
			sysBrand.setCreateTimes(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(sysBrand.getCreateTime()));
		}
		if(sysBrand.getToExamineTime()!=null){
			sysBrand.setToExamineTimes(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(sysBrand.getToExamineTime()));
		}
		if(sysBrand.getUpdateTime()!=null){
			sysBrand.setUpdateTimes(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(sysBrand.getUpdateTime()));
		}




		model.addAttribute("sysBrand", sysBrand);
		return "jzweb/sysBrand/details";
	}


	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:sysBrand:edit")
	String edit(@PathVariable("id") Long id,Model model){
		SysBrandDO sysBrand = sysBrandService.selectById(id);
		model.addAttribute("sysBrand", sysBrand);
		Map map1=new HashMap();

		List <Map> mapList=new ArrayList<>();
		map1.put("name","幼儿照护");
		mapList.add(map1);
		Map map2=new HashMap();
		map2.put("name","产后恢复");
		mapList.add(map2);
		model.addAttribute("list2",mapList);
	    return "jzweb/sysBrand/edit";
	}

	@PostMapping("/review")
	@ResponseBody
	@RequiresPermissions("jzweb:sysBrand:edit")
	Result<String> review(@RequestParam("ids[]") Long[] ids,Integer state,String remark){
		      int p=0;
		if (state==1 ||state==2) {
			for (Long id:ids){
				SysBrandDO sysBrandDO1=	sysBrandService.selectById(id);
				if(!sysBrandDO1.getStatus().equals(1)&&!sysBrandDO1.getStatus().equals(2)){
					SysBrandDO sysBrandDO=new SysBrandDO();
					sysBrandDO.setId(id);
					sysBrandDO.setStatus(state);
					sysBrandDO.setToExamineTime(LocalDateTime.now());
					sysBrandDO.setToExamine(getUserId());
					sysBrandDO.setToExamineName(getUsername());
					sysBrandDO.setRemarks(remark);
					boolean b=	sysBrandService.updateById(sysBrandDO);
					if(b){
						p++;
					}
				}

			}
			if (p == ids.length) {
				return Result.build(0,"已成功审核：" + p + "条数据");
			} else {
				return Result.fail("有审核失败的记录/或者重复审核的记录，"+"成功审核"+p+"请查看！");
			}
		}else {
			return Result.fail("存在重复审核数据！");
		}

	}



	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:sysBrand:add")
	public Result<String> save( SysBrandDO sysBrand){
		sysBrand.setCreateTime(LocalDateTime.now());
		sysBrandService.insert(sysBrand);
        return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:sysBrand:edit")
	public Result<String>  update( SysBrandDO sysBrand){
		boolean update = sysBrandService.updateById(sysBrand);
		return update ? Result.ok() : Result.fail();
	}

	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:sysBrand:remove")
	public Result<String>  remove( Long id){
		sysBrandService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:sysBrand:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		sysBrandService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
